﻿namespace MDXStudio
{
    using System;
    using System.Windows.Forms;

    internal class PerfStats
    {
        private RichTextBox DisplayTextBox;
        private DateTime dtStart;
        private ASPerfCounter[] PerfCounters;
        private TimeSpan tsDuration;

        public PerfStats(RichTextBox TextBox)
        {
            this.DisplayTextBox = TextBox;
        }

        public void Clear()
        {
            this.DisplayTextBox.Clear();
        }

        public void Display()
        {
            string str = (this.tsDuration.Hours > 0) ? (this.tsDuration.Hours + " hr") : "";
            if (this.tsDuration.Minutes > 0)
            {
                object obj2 = str;
                str = string.Concat(new object[] { obj2, (str == "") ? "" : " ", this.tsDuration.Minutes, " min" });
            }
            if (this.tsDuration.Seconds > 0)
            {
                object obj3 = str;
                str = string.Concat(new object[] { obj3, (str == "") ? "" : " ", this.tsDuration.Seconds, " sec" });
            }
            if (this.tsDuration.Milliseconds > 0)
            {
                object obj4 = str;
                str = string.Concat(new object[] { obj4, (str == "") ? "" : " ", this.tsDuration.Milliseconds, " ms" });
            }
            this.DisplayTextBox.AppendText(string.Format("Time              : {0}\n", str));
            foreach (ASPerfCounter counter in this.PerfCounters)
            {
                this.DisplayTextBox.AppendText(string.Format("{0} : {1}\n", counter.GetDescription(), counter.GetValueText()));
            }
        }

        internal void DisplaySomeData(string name, string val)
        {
            this.DisplayTextBox.AppendText(string.Format("{0}\t:{1}\n", name, val));
        }

        internal void DisplayTitle(string title)
        {
            this.DisplayTextBox.AppendText(string.Format("{0}\n", title));
        }

        public void Finish()
        {
            foreach (ASPerfCounter counter in this.PerfCounters)
            {
                counter.Finish();
            }
            this.tsDuration = (TimeSpan) (DateTime.Now - this.dtStart);
        }

        public void Start(string ServerName, string version)
        {
            this.PerfCounters = new ASPerfCounter[] { new ASPerfCounter(ServerName, version, "MDX", "Total calculation covers", "Calc covers      "), new ASPerfCounter(ServerName, version, "MDX", "Total cells calculated", "Cells calculated "), new ASPerfCounter(ServerName, version, "MDX", "Total Sonar subcubes", "Sonar subcubes   "), new ASPerfCounter(ServerName, version, "MDX", "Total NON EMPTY", "NON EMPTYs       "), new ASPerfCounter(ServerName, version, "MDX", "Total Autoexist", "Autoexists       "), new ASPerfCounter(ServerName, version, "MDX", "Total EXISTING", "EXISTINGs        "), new ASPerfCounter(ServerName, version, "Storage Engine Query", "Total measure group queries", "SE queries       "), new ASPerfCounter(ServerName, version, "MDX", "Total flat cache inserts", "Flat cache insert"), new ASPerfCounter(ServerName, version, "Cache", "Total direct hits", "Cache hits       "), new ASPerfCounter(ServerName, version, "Cache", "Total misses", "Cache misses     "), new ASPerfCounter(ServerName, version, "Cache", "Total inserts", "Cache inserts    "), new ASPerfCounter(ServerName, version, "Cache", "Total lookups", "Cache lookups    "), new ASPerfCounter(ServerName, version, "Memory", "Memory Usage KB", "Memory Usage KB  ") };
            foreach (ASPerfCounter counter in this.PerfCounters)
            {
                counter.Start();
            }
            this.dtStart = DateTime.Now;
        }

        internal long Duration
        {
            get
            {
                return (long) this.tsDuration.TotalMilliseconds;
            }
        }
    }
}

